package com.lujianbuping.system.opportunityprojectmanagementsystem.utils.array;

import java.lang.reflect.Array;


public class ArrayUtils {

	/**
	 * 复制任意类型一维数组，包括基础类型的数组，如int[]
	 * 
	 * @param arr
	 * @param newLength
	 * @return
	 */
	public static Object copyOf(Object arr, int newLength) {
		if (arr == null) {
			return null;
		}
		Class<?> clz = arr.getClass();
		if (!clz.isArray()) {
			return null;
		}
		Class<?> type = clz.getComponentType();
		int length = Array.getLength(arr);
		Object newArray = Array.newInstance(type, newLength);
		System.arraycopy(arr, 0, newArray, 0, Math.min(length, newLength));
		return newArray;
	}

	/**
	 * 任意类型，任意维度数组深拷贝
	 * 
	 * @param arr
	 * @param newLength
	 * @return
	 */
	public static Object deepAnyArrayCopyOf(Object arr, int newLength) {
		if (arr == null) {
			return null;
		}
		Class<?> clz = arr.getClass();
		if (!clz.isArray()) {
			return null;
		}
		Class<?> type = clz.getComponentType();
		int length = Array.getLength(arr);
		Object newArray = Array.newInstance(type, newLength);

		for (int i = 0; i < Math.min(length, newLength); i++) {
//			newArray[i].getClass()
//			if() {
//				
//			}
		}

//		System.arraycopy(arr, 0, newArray, 0, Math.min(length, newLength));

		return null;
	}

	public static void main(String[] args) {
		int[][] arr = new int[][] { { 2 }, null };

		int[][] arr2 = (int[][]) copyOf(arr, 20);

		System.out.println(arr2[0].length);
		for (int[] i : arr2) {
			System.out.println(i);
			for (int js : i) {
				System.out.println(js);
			}
		}

	}

}
